/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package engine;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import xml.XMLController;

/**
 *
 * @author bi
 */
public final class CommandCombat {

    private class CommandCombatEach {

        public JComboBox player;
        public JComboBox faction;
        private ActionListener playerListener;

        public CommandCombatEach(final int row) {
            player = new JComboBox(pList);
            faction = new JComboBox(new String[]{Constant.COMMAND_DEFAULT});

            playerListener = new ActionListener() {

                public void actionPerformed(ActionEvent evt) {
                    if (!list.get(list.size() - 1).player.getSelectedItem().toString().equals(Constant.COMMAND_DEFAULT)) {
                        addCommand();
                    }
                    removeCommand();
                    addFactionCommand((JComboBox)evt.getSource());
                }
            };
            player.addActionListener(playerListener);
        }
    }
    private JPanel parent;
    private XMLController docPlayer, docFaction;
    private Vector<CommandCombatEach> list;
    private Vector<String> pList;

    public CommandCombat(JPanel parent,
            XMLController docPlayer,
            XMLController docFaction) {
        this.parent = parent;
        load(docPlayer, docFaction);
    }

    public void load(XMLController docPlayer,
            XMLController docSquad) {
        pList = new Vector<String>();
        pList.add(Constant.COMMAND_DEFAULT);
        for (int i = 0; i < docPlayer.table.length; i++) {
            if (docPlayer.table[i][3].equals("Human")) {
                pList.add(docPlayer.table[i][0]);
            }
        }

        this.docPlayer = docPlayer;
        this.docFaction = docSquad;

        parent.removeAll();
        list = new Vector<CommandCombatEach>();
        addCommand();
    }

    public void addCommand() {
        CommandCombatEach cce = new CommandCombatEach(list.size());
        list.add(cce);
        parent.add(cce.player);
        parent.add(cce.faction);
        parent.setLayout(new GridLayout(list.size(), 2, 50, 30));
    }

    public void removeCommand() {
        for (int c = 0; c < list.size() - 1; c++) {
            CommandCombatEach cce = list.get(c);
            if (list.get(c).player.getSelectedItem().toString().equals(Constant.COMMAND_DEFAULT)) {
                list.remove(cce);
                parent.remove(cce.player);
                parent.remove(cce.faction);
                parent.setLayout(new GridLayout(list.size(), 3, 50, 30));
                c--;
            }
        }
    }

    public void addFactionCommand(JComboBox source) {
        for (int c = 0; c < list.size(); c++) {
            CommandCombatEach cce = list.get(c);
            if (cce.player.equals(source)) {
                String playerName = cce.player.getSelectedItem().toString();
                String factionName = null;
                String teamName = null;

                for (int i = 0; i < docPlayer.table.length && factionName == null; i++) {
                    if (docPlayer.table[i][0].equals(playerName)) {
                        factionName = docPlayer.table[i][1];
                    }
                }

                for (int i = 0; i < docFaction.table.length && teamName == null; i++) {
                    if (docFaction.table[i][2].equals(factionName)) {
                        teamName = docFaction.table[i][1];
                    }
                }

                Vector<String> targetName = new Vector<String>();
                targetName.add(Constant.COMMAND_RANDOM);

                for (int i = 0; i < docFaction.table.length; i++) {
                    if (!docFaction.table[i][1].equals(teamName)
                            && !targetName.contains(docFaction.table[i][1])) {
                        targetName.add(docFaction.table[i][1]);
                    }
                }

                cce.faction.setModel(new DefaultComboBoxModel(targetName));
            }
        }
    }

    public String[][] getCommand() {
        String[][] commandList = new String[list.size() - 1][2];
        for (int i = 0; i < list.size() - 1; i++) {
            commandList[i][0] = list.get(i).player.getSelectedItem().toString();
            commandList[i][1] = list.get(i).faction.getSelectedItem().toString();
        }
        return commandList;
    }
}
